package com.atguign.gulimall.member.service.impl;

import com.atguign.gulimall.member.dao.MemberDao;
import com.atguign.gulimall.member.dao.MemberLevelDao;
import com.atguign.gulimall.member.entity.MemberEntity;
import com.atguign.gulimall.member.entity.MemberLevelEntity;
import com.atguign.gulimall.member.exception.PhoneExsitException;
import com.atguign.gulimall.member.exception.UsernameExistException;
import com.atguign.gulimall.member.service.MemberService;
import com.atguign.gulimall.member.vo.MemberLoginVo;
import com.atguign.gulimall.member.vo.MemberRegistVo;
import com.atguign.gulimall.member.vo.UserVo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    public MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo memberRegistVo) throws PhoneExsitException, UsernameExistException {

        if (baseMapper.selectCount(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getMobile, memberRegistVo.getPhone())) > 0) {
            throw new PhoneExsitException();
        }

        if (baseMapper.selectCount(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getUsername, memberRegistVo.getUsername())) > 0) {
            throw new UsernameExistException();
        }


        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername(memberRegistVo.getUsername());
        memberEntity.setMobile(memberRegistVo.getPhone());
        memberEntity.setCreateTime(new Date());
        //设置默认等级
        MemberLevelEntity memberLevelEntity =
                memberLevelDao.selectOne(new LambdaQueryWrapper<MemberLevelEntity>().eq(MemberLevelEntity::getDefaultStatus, 1));
        memberEntity.setLevelId(memberLevelEntity.getId());
        //设置密码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        memberEntity.setPassword(bCryptPasswordEncoder.encode(memberRegistVo.getPassword()));


        this.baseMapper.insert(memberEntity);
    }

    @Override
    public MemberEntity login(MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = baseMapper.selectOne(new LambdaQueryWrapper<MemberEntity>()
                .eq(MemberEntity::getUsername, memberLoginVo.getLoginacct())
                .or()
                .eq(MemberEntity::getMobile, memberLoginVo.getLoginacct()));
        if (memberEntity != null) {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            if (bCryptPasswordEncoder.matches(memberLoginVo.getPassword(), memberEntity.getPassword())) {
                return memberEntity;
            }
        }
        return null;
    }

    @Override
    public MemberEntity login(UserVo userVo) {
        MemberEntity memberEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getSocialUid, userVo.getId()));
        if (memberEntity != null) {
            return memberEntity;
        } else {
            try {
                MemberEntity insert = new MemberEntity();
                //设置默认等级
                MemberLevelEntity memberLevelEntity =
                        memberLevelDao.selectOne(new LambdaQueryWrapper<MemberLevelEntity>().eq(MemberLevelEntity::getDefaultStatus, 1));
                insert.setSocialUid(String.valueOf(userVo.getId()));
                insert.setLevelId(memberLevelEntity.getId());
                insert.setNickname(userVo.getName());
                insert.setCreateTime(new Date());

                this.baseMapper.insert(insert);
                return insert;
            } catch (RuntimeException e) {
                log.error("社交账号登录失败", e);
                return null;
            }

        }
    }

}